var express = require('express');
var router = express.Router();
//引入学生的业务层
var studentService = require("../src/services/student/studentService");

router.get("/showInfos",(req,resp)=>{

    //调用学生的业务层的显示所有学生的方法
   let result = studentService.showAllStudentsInfo();
   result.then((data)=>{
      resp.end(JSON.stringify(data));
   })
 });

 //处理添加
 router.post("/add",(req,resp)=>{
    //获得客户端的参数
    let name = req.body.name;
    let age = req.body.age;
    let gender = req.body.gender;
    let score = req.body.score;
    let obj = {"name":name,"age":age,"gender":gender,"score":score}
  let result = studentService.addStudentInfo(obj);
  result.then(data=>{
     //console.log(data);
     if(data!=null){
      resp.end(JSON.stringify({"code":0,"msg":"添加成功"}))
     }else{
      resp.end(JSON.stringify({"code":1,"msg":"添加失败"}))
     }
      
  })



});

router.post("/del",(req,resp)=>{
   //获得删除的id
   let id = req.body.studentId;
  let result=studentService.deleteStudentInfo(id);
  result.then(data=>{
     //console.log(data);
     if(data.deletedCount==1){
      resp.end(JSON.stringify({"code":0,"msg":"删除成功"}))
     }else{
      resp.end(JSON.stringify({"code":1,"msg":"删除失败"}))
     }
  });
});

module.exports = router;